package cn.chinaflame.pagodas.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import cn.chinaflame.pagodas.entity.PaEmailLog;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.repository.PaEmailLogRepository;
import cn.chinaflame.pagodas.utils.ParamPreconditions;
import cn.chinaflame.pagodas.utils.stl.DateUtils;

/**
 *
 * @author pengcheng.zhao
 */
@Service
public class PaEmailLogService {
	 private static final Logger logger = LoggerFactory.getLogger(PaAgentService.class);
	    @Autowired
	    private JdbcTemplate jdbcTemplate;
	    
	    /**
	     * 插入邮件日志
	     * @param log
	     * @return
	     */
	    public int addEmail(PaEmailLog log) {
	    	logger.info("PaEmailLogService:addEmail:邮箱bean:{}",JSONObject.toJSONString(log));
	    	String sql ="insert into pa_email_log(receiver,subject,content,copyto,createtime,result,attach_file)values(?,?,?,?,now(),?,?)";
	    	int result = jdbcTemplate.update(sql,new Object[] {log.getReceiver(),log.getSubject(),log.getContent(),log.getCopyto(),log.getResult(),log.getAttachFile()});
	    	logger.info("PaEmailLogService:addEmail:result={}",result);
	    	return result;
	    }
	/**
	 * 通过日期查询
	 * @param date
	 * @return
	 */
    public List<PaEmailLog> findByCreatetime(String date) {
    	logger.info("EmailLogService:findByCreatetime:date:{}",date);
    	ParamPreconditions.checkDateByFmt(date, DateUtils.DATE_DEFAULT_PATTERN);
    	String sql = "select * from pa_email_log where date_format(createtime,'%Y-%m-%d')= date_format(?,'%Y-%m-%d')";
    	List<PaEmailLog> list = jdbcTemplate.query(sql, new Object[] {date}, (RowMapper<PaEmailLog>) (rs, rowNum) -> {
    		PaEmailLog result = new PaEmailLog();
			result.setContent(rs.getString("content"));
			result.setCopyto(rs.getString("copyto"));
			result.setCreatetime(rs.getString("createtime"));
			result.setReceiver(rs.getString("receiver"));
			result.setResult(rs.getString("result"));
			return result;
		});
    	logger.info("PaEmailLogService:findByCreatetime:list.size()={}",list.size());
		return list;
	}
    
    /**
     * 通过主题查询
     * @param subject
     * @return
     */
    public List<PaEmailLog> findBySubject(String subject) {
    	logger.info("EmailLogService:findBySubject:subject={}",subject);
    	ParamPreconditions.checkNotEmpty(subject, CodeEnum.FAIL.getCode(),"邮件主题不能为空！");
    	String sql = "select * from pa_email_log where subject=?";
    	List<PaEmailLog> list = jdbcTemplate.query(sql,new Object[] {subject},  (RowMapper<PaEmailLog>) (rs, rowNum) -> {
    		PaEmailLog result = new PaEmailLog();
			result.setContent(rs.getString("content"));
			result.setCopyto(rs.getString("copyto"));
			result.setCreatetime(rs.getString("createtime"));
			result.setReceiver(rs.getString("receiver"));
			result.setResult(rs.getString("result"));
			return result;
		});
    	logger.info("EmailLogService:findBySubject:list.size()={}",list.size());
		return list;
	}
    
    /**
     * 通过收件人查询
     * @param receiver
     * @return
     */
    public List<PaEmailLog> findByReceiver(String receiver) {
    	logger.info("EmailLogService:findByReceiver:receiver={}",receiver);
    	ParamPreconditions.checkNotEmpty(receiver, CodeEnum.FAIL.getCode(),"邮件收件人不能为空！");
    	String sql = "select * from pa_email_log where receiver=?";
    	List<PaEmailLog> list = jdbcTemplate.query(sql, new Object[] {receiver}, (RowMapper<PaEmailLog>) (rs, rowNum) -> {
    		PaEmailLog result = new PaEmailLog();
			result.setContent(rs.getString("content"));
			result.setCopyto(rs.getString("copyto"));
			result.setCreatetime(rs.getString("createtime"));
			result.setReceiver(rs.getString("receiver"));
			result.setResult(rs.getString("result"));
			return result;
		});
    	logger.info("EmailLogService:findByReceiver:list.size()={}",list.size());
		return list;
	}
    
    /**
     * 通过发送结果查询
     * @param result
     * @return
     */
    public List<PaEmailLog> findByResult(String ret) {
    	logger.info("EmailLogService:findByResult:result={}",ret);
    	ParamPreconditions.checkNotEmpty(ret, CodeEnum.FAIL.getCode(),"邮件发送结果不能为空！");
    	String sql = "select * from pa_email_log where result=?";
    	List<PaEmailLog> list = jdbcTemplate.query(sql,new Object[] {ret}, (RowMapper<PaEmailLog>) (rs, rowNum) -> {
    		PaEmailLog result = new PaEmailLog();
			result.setContent(rs.getString("content"));
			result.setCopyto(rs.getString("copyto"));
			result.setCreatetime(rs.getString("createtime"));
			result.setReceiver(rs.getString("receiver"));
			result.setResult(rs.getString("result"));
			return result;
		});
    	logger.info("EmailLogService:findByResult:list.size()={}",list.size());
		return list;
	}
    
    /**
     * 通过抄送人查询
     * @param result
     * @return
     */
    public List<PaEmailLog> findByCopyto(String copyto) {
    	logger.info("EmailLogService:findByCopyto:copyto={}",copyto);
//    	ParamPreconditions.checkNotEmpty(copyto, CodeEnum.FAIL.getCode(),"邮件抄送人不能为空！");
    	String sql = "select * from pa_email_log where copyto=?";
    	List<PaEmailLog> list = jdbcTemplate.query(sql,new Object[] {copyto}, (RowMapper<PaEmailLog>) (rs, rowNum) -> {
    		PaEmailLog result = new PaEmailLog();
			result.setContent(rs.getString("content"));
			result.setCopyto(rs.getString("copyto"));
			result.setCreatetime(rs.getString("createtime"));
			result.setReceiver(rs.getString("receiver"));
			result.setResult(rs.getString("result"));
			return result;
		});
    	logger.info("EmailLogService:findByCopyto:list.size()={}",list.size());
		return list;
	} 
}
